#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'
import os
import uuid

class AssetObject(object):
    def __init__(self, **kwargs):
        for attr in kwargs:
            setattr(self, attr, kwargs[attr])
    def __repr__(self):
        return "<class %s: %s>" %(self.__class__.__name__, [attr for attr in dir(self) if '__' not in attr])


def get_asset_library_data(library_path):
    library_data = None
    if not os.path.isdir(library_path):
        return
    base_name = os.path.basename(library_path)
    item_uuid = uuid.uuid4().hex
    # asset_types = get_asset_type_data(asset_library_name=base_name, asset_library_path=path)
    library_data = AssetObject(name = base_name,
                                path = library_path,
                                item_uuid=item_uuid,
                                item_type ='library')
    return library_data

def get_asset_type_data(node):
    asset_library_path = node.path
    asset_library_name = node.name
    asset_type_data = []
    file_name_list = os.listdir(asset_library_path)
    if file_name_list:
        for file_name in file_name_list:
            file_path = os.path.join(asset_library_path, file_name)
            if os.path.isdir(file_path):
                item_uuid = uuid.uuid4().hex
                # asset_list = get_asset_name_data(asset_type_name=file_name, asset_type_path=file_path, asset_library_name=asset_library_name, asset_library_path=asset_library_path)
                asset_type_data.append(AssetObject(name=file_name,
                                                   path=file_path,
                                                   asset_library_name=asset_library_name,
                                                   asset_library_path=asset_library_path,
                                                   item_uuid=item_uuid,
                                                   item_type='type'))
    # node.asset_type_list = asset_type_data
    return asset_type_data

def get_asset_name_data(node):
    asset_type_path = node.path
    asset_name_data = []
    file_name_list = os.listdir(asset_type_path)
    if file_name_list:
        for file_name in file_name_list:
            file_path = os.path.join(asset_type_path, file_name)
            if os.path.isdir(file_path):
                item_uuid = uuid.uuid4().hex
                asset_name_data.append(AssetObject(name=file_name,
                                                   path=file_path,
                                                   asset_type_name=node.name,
                                                   asset_library_name=node.asset_library_name,
                                                   asset_library_path=node.asset_library_path,
                                                   item_uuid=item_uuid,
                                                   item_type='asset'))
    return asset_name_data

def get_asset_step_data(node):
    step_list = ['designs', 'alembics', 'shaders', 'textures', 'rigs', 'loops']
    asset_path = node.path
    asset_step_data = {}
    for step in step_list:
        file_path = os.path.join(asset_path, step)
        if os.path.isdir(file_path):
            data = {}
            item_list = []
            file_name_list = os.listdir(file_path)
            if file_name_list:
                if step == 'textures':
                    for task_name in file_name_list:
                        data = {}
                        task_path = os.path.join(asset_path, step, task_name)
                        if not os.path.isdir(task_path):
                            continue
                        version_file_list = os.listdir(task_path)
                        version_file_list.reverse()
                        if not version_file_list:
                            continue
                        for version_file in version_file_list:
                            if not version_file.endswith('.exr'):
                                continue
                            type_group = version_file[:-9]
                            if data.get(type_group):
                                data[type_group].append(version_file)
                            else:
                                data[type_group] = [version_file]
                        if not data:
                            continue
                        for key in data:
                            version_file_list = data.get(key)
                            version_list = []
                            for version_file in version_file_list:
                                item_uuid = uuid.uuid4().hex
                                version = version_file.split('.')[-2]
                                chiled_item = AssetObject(name=task_name + ' : ' + key,
                                                          path=os.path.join(task_path, version_file),
                                                          version=version,
                                                          asset_step=step,
                                                          asset_name=node.name,
                                                          asset_type_name=node.asset_type_name,
                                                          asset_library_name=node.asset_library_name,
                                                          asset_library_path=node.asset_library_path,
                                                          item_uuid=item_uuid,
                                                          item_type='variant')
                                version_list.append(chiled_item)
                            version_file = version_file_list[0]
                            version = version_file.split('.')[-2]
                            item_uuid = uuid.uuid4().hex
                            item = AssetObject(name=task_name + ' : ' + key,
                                               path=os.path.join(task_path, version_file),
                                               version=version,
                                               asset_step=step,
                                               asset_name=node.name,
                                               asset_type_name=node.asset_type_name,
                                               asset_library_name=node.asset_library_name,
                                               asset_library_path=node.asset_library_path,
                                               version_list=version_list,
                                               item_uuid=item_uuid,
                                               item_type='variant')
                            item_list.append(item)
                    if item_list:
                        asset_step_data[step] = item_list
                else:
                    file_name_list.reverse()
                    if step == 'designs':
                        file_type_list = ['png']
                    else:
                        file_type_list = ['ma', 'abc']
                    for file_name in file_name_list:
                        if file_name.split('.')[-1] not in file_type_list:
                            continue
                        type_group = file_name.split('.')[-3]
                        if data.get(type_group):
                            data[type_group].append(file_name)
                        else:
                            data[type_group] = [file_name]
                    for key in data:
                        version_file_list = data.get(key)
                        version_list = []
                        for version_file in version_file_list:
                            item_uuid = uuid.uuid4().hex
                            version = version_file.split('.')[-2]
                            chiled_item = AssetObject(name=version_file.split(key,1)[0]+key,
                                                      path=os.path.join(asset_path, step, version_file),
                                                      version=version,
                                                      asset_step=step,
                                                      asset_name=node.name,
                                                      asset_type_name=node.asset_type_name,
                                                      asset_library_name=node.asset_library_name,
                                                      asset_library_path=node.asset_library_path,
                                                      item_uuid=item_uuid,
                                                      item_type='variant')
                            version_list.append(chiled_item)
                        version_file = version_file_list[0]
                        version = version_file.split('.')[-2]
                        item_uuid = uuid.uuid4().hex
                        item = AssetObject(name=version_file.split(key,1)[0]+key,
                                           path=os.path.join(asset_path, step, version_file),
                                           version=version,
                                           asset_step=step,
                                           asset_name=node.name,
                                           asset_type_name=node.asset_type_name,
                                           asset_library_name=node.asset_library_name,
                                           asset_library_path=node.asset_library_path,
                                           version_list=version_list,
                                           item_uuid=item_uuid,
                                           item_type='variant')
                        item_list.append(item)
                    asset_step_data[step] = item_list
    return asset_step_data

class SearchAsset():
    def __init__(self, root_path, text):
        self._root_path = root_path
        self._search_text = text
        self.asset_data_list = list()

    def _search_asset_data(self, *arg):
        for file_name in arg[-1]:
            if arg[0] in file_name:
                if file_name.endswith('.mb') or file_name.endswith('.ma'):
                    asset_type = os.path.basename(os.path.dirname(arg[1]))
                    asset_name = os.path.basename(arg[1])
                    asset_variant = file_name
                    file_path = os.path.join(arg[1], file_name)
                    thumbnail = file_path.replace(file_name[-3:], '.png')
                    self.asset_data_list.append(AssetObject(asset_variant=asset_variant, path=file_path, thumbnail=thumbnail, asset_name=asset_name, asset_type=asset_type))
        self.asset_data_list.sort()
        return self.asset_data_list

    def search(self):
        self.asset_data_list = list()
        os.path.walk(self._root_path, self._search_asset_data, self._search_text)
        return self.asset_data_list
